package boxuegu.com.boxuegu2.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import boxuegu.com.boxuegu2.R;

public class AdBannerFragment extends Fragment {//一个自定义碎片对象表示一片广告
    private ImageView imageView;
    private String ab;

    public static AdBannerFragment newInstance(String ab){//参数ab:表示广告图片的名字
//        this.ab=ab;这时对象还没有生成，不能为该对象的属性赋值
        AdBannerFragment af=new AdBannerFragment();
        af.ab=ab;
        return af;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {//当new对象时自动执行
        super.onCreate(savedInstanceState);
    }
    @Nullable
    @Override
    //当需要生成碎片外观的时候执行,返回碎片外观
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //该处代码一般用来初始化界面，例如加载布局文件，或用Java代码生成界面元素
        imageView=new ImageView(getActivity());//参数为给界面元素所处的activity对象
        //p181，下面可以为imageView添加属性宽高
        ViewGroup.LayoutParams lp=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(lp);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);//当imageView的尺寸大于图片尺寸，则拉升充满
        return imageView;
    }
    @Override
    //当碎片界面可用时执行，一般执行在onCreateView
    public void onResume() {
        //该处一般用来为界面元素添加内容
        super.onResume();
        if(ab.equals("banner_1")){
            imageView.setImageResource(R.drawable.banner_1);
        }else if(ab.equals("banner_2")){
            imageView.setImageResource(R.drawable.banner_2);
        }else if(ab.equals("banner_3")){
            imageView.setImageResource(R.drawable.banner_3);
        }
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

}
